define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var tools = require("tools");
	
    var instance = {
        initComponent: function (params,widget) {
        	var gridEnd = new Grid({
                parentNode: ".J-gridEnd", 
                autoRender:false,
                model : {
                	url : "api/dinner/query",
                	params : function(){
                		return {
                			"checkInDinner.pkCheckInDinner":widget.get("pkCheckInDinner"),
                			"orderString":"dateOfDinner:desc",
                			"isValid":true,
                			fetchProperties : "pkDinner,version,dateOfDinner,dinnerSort.*,status.*,dinnerDate,perNumber," +
                					"level,dinnerAddress,taste,preference,species,drinks,isValid"
                		}
                	},
                	head : {
						items : [ {
							id : "setAll",
							type : "button",
							text : "全部落实",
							handler : function() {
								var gridData = widget.get("gridEnd").getData();
								var flag = true;
								for(var i in gridData){
									if(gridData[i].status.key == "Pending"){
										flag = false;
									}
								}
								if(!flag){
									aw.ajax({
										url : "api/dinner/penddinner",
										data : {
											pkCheckInDinner : widget.get("pkCheckInDinner"),
										},
										dataType : "json",
										success : function(datas){
											widget.get("gridEnd").refresh();
										}
									})
								}else{
									Dialog.alert({
										tip : "提示",
										content : "该团所有房间都已落实！"
									});
									return false;
								}
							}
						} ]
					},
                    columns : [{
                    		name : "dinnerSort.value",
                    		label : "餐饮类别",
                    	},{
                        	name:"dateOfDinner",
                        	format:"date",
            				label:"用餐日期"
                    	},{
                        	name:"dinnerDate",
            				label:"用餐时间"
                    	},{
                    		name:"perNumber",
            				label:"用餐人数",
                    	},{
                    		name:"level",
            				label:"标准",
                    	},{
                    		name:"dinnerAddress",
            				label:"用餐地点",
                    	},{
                    		name:"taste",
            				label:"口味（客源地）",
                    	},{
                    		name:"preference",
            				label:"偏好",
                    	},{
                    		name:"species",
            				label:"指定菜",
                    	},{
                    		name:"drinks",
            				label:"酒水饮料",
                    	},{
							name:"operate",
							label:"操作",
							format:"button",
							formatparams:[{
								key:"edit",
								text:"落实",
								show:function(value,data){
									if (data.status.key == "Pending") {
										return true;
				                    } else {
				                        return false;
				                    }
								},
								handler:function(index,data,rowEle){
									aw.ajax({
										url : "api/dinner/penddinner",
										data : {
											pkCheckInDinner : widget.get("pkCheckInDinner"),
											pkDinner: data.pkDinner
										},
										dataType : "json",
										success : function(datas){
											widget.get("gridEnd").refresh();
										}
									})
								}
							}]
                    	}]
                }
            });
            widget.set("gridEnd",gridEnd);
        }
    };
    module.exports = instance;
});